package com.giscloud.amp.service.auth.impl;

import com.giscloud.amp.dao.AmpOrgMapper;
import com.giscloud.amp.entity.AmpOrg;
import com.giscloud.amp.entity.AmpOrgExample;
import com.giscloud.amp.entity.dto.AmpOrgDTO;
import com.giscloud.amp.entity.vo.AmpOrgTreeVO;
import com.giscloud.amp.entity.vo.AmpOrgVO;
import com.giscloud.amp.entity.vo.OrgTreeVO;
import com.giscloud.amp.service.auth.AmpOrgService;
import com.giscloud.commons.model.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author:gxd
 * @Description: 权限服务类
 * @Date: 17:19 2018/2/24
 * @Modified By:
 */
@Service("ampOrgService")
@CacheConfig(cacheNames ="amp:ampOrgService")
public class AmpOrgServiceImpl implements AmpOrgService {
    private final static Logger log = LoggerFactory.getLogger(AmpOrgServiceImpl.class);
    @Autowired
    private AmpOrgMapper ampOrgMapper;

    /**
     * 根据对象类型、对象值获取权限信息(用户、角色。)
     * @param ampOrgDTO
     * @return
     */
    @Override
    public List<AmpOrgVO> findAmpOrgList(AmpOrgDTO ampOrgDTO) {
        AmpOrgExample staffAuthExample = new AmpOrgExample();
        AmpOrgExample.Criteria criteria = staffAuthExample.createCriteria();
        criteria.andStatusCdEqualTo((short)1);
        staffAuthExample.setDistinct(true);
        AmpOrg ampOrg = new AmpOrg();
        BeanUtils.copyProperties(ampOrgDTO,ampOrg);
        return ampOrgMapper.findOrgByExample(ampOrg,staffAuthExample);
    }

    @Override
    public List<AmpOrgVO> findOrgTree(AmpOrgDTO ampOrgDTO) {
        AmpOrg ampOrg = new AmpOrg();
        BeanUtils.copyProperties(ampOrgDTO,ampOrg);
        return ampOrgMapper.findOrgTree(ampOrg);
    }

    @Override
    public List<AmpOrgVO> findOrgByOrgName(AmpOrgDTO ampOrgDTO) {
        AmpOrg ampOrg = new AmpOrg();
        BeanUtils.copyProperties(ampOrgDTO,ampOrg);
        return ampOrgMapper.findOrgByOrgName(ampOrg);
    }

    @Override
    public List<AmpOrgVO> findOrgByOrgId(AmpOrgDTO ampOrgDTO) {
        AmpOrg ampOrg = new AmpOrg();
        BeanUtils.copyProperties(ampOrgDTO,ampOrg);
        return ampOrgMapper.findOrgByOrgId(ampOrg);
    }

    @Override
    public List<OrgTreeVO> listOrgTree(AmpOrgDTO ampOrgDTO) {
        return ampOrgMapper.listOrgTreeByDTO(ampOrgDTO);
    }

    @Override
    public Result insert(AmpOrgDTO ampOrgDTO) {
        AmpOrgExample ampOrgExample = new AmpOrgExample();
        ampOrgExample.createCriteria().andStatusCdEqualTo((short)1).andOrgCodeEqualTo(ampOrgDTO.getOrgCode());
        List<AmpOrg> ampOrgs = ampOrgMapper.selectByExample(ampOrgExample);
        if(CollectionUtils.isNotEmpty(ampOrgs)){
            return Result.error("机构编码已存在");
        }
        AmpOrg record = new AmpOrg();
        BeanUtils.copyProperties(ampOrgDTO,record);
        record.setCreatedDt(new Date());
        record.setVersion(new Date());
        record.setStatusCd((short) 1);
        record.setOrgType((short) 2);
        ampOrgMapper.insertSelective(record);
        return Result.success("","添加成功");
    }

    @Override
    public Result update(AmpOrgDTO ampOrgDTO) {
        if(StringUtils.isNotBlank(ampOrgDTO.getOrgCode())){
            AmpOrgExample ampOrgExample = new AmpOrgExample();
            ampOrgExample.createCriteria().andStatusCdEqualTo((short)1)
                    .andOrgCodeEqualTo(ampOrgDTO.getOrgCode()).andOrgIdNotEqualTo(ampOrgDTO.getOrgId());
            List<AmpOrg> ampOrgs = ampOrgMapper.selectByExample(ampOrgExample);
            if(CollectionUtils.isNotEmpty(ampOrgs)){
                return Result.error("机构编码已存在");
            }
        }
        AmpOrgExample example = new AmpOrgExample();
        AmpOrg record = new AmpOrg();
        BeanUtils.copyProperties(ampOrgDTO,record);
        example.createCriteria().andOrgIdEqualTo(ampOrgDTO.getOrgId());
        record.setVersion(new Date());
        ampOrgMapper.updateByExampleSelective(record,example);
        return Result.success("","更新成功");
    }

    @Override
    public int delete(Long orgId) {
        AmpOrgExample example = new AmpOrgExample();
        example.createCriteria().andOrgIdEqualTo(orgId);
        return ampOrgMapper.deleteByExample(example);
    }

    @Override
    public List<AmpOrgTreeVO> findAmpOrgTree(AmpOrgDTO ampOrgDTO) {
        return ampOrgMapper.findAmpOrgTree(ampOrgDTO);
    }
}
